<?php

namespace app\yy\admin;

use app\admin\controller\Admin;
use app\yy\model\DomainHistory;
use app\yy\model\Website as WebModel;
use app\yy\model\Domain as DoModel;

use app\common\builder\ZBuilder;

class Domain extends Admin
{
    public $status = [
        '0' => '禁用',
        '1' => '启用',
    ];

    public function index()
    {
        // 获取查询条件
        $map = $this->getMap();
        $data_list = WebModel::alias('w')
            ->join('domain d', 'w.domain=d.domain', 'left')
            ->field('d.*,w.domain,w.id as wid')
            ->order('w.id desc')->where($map)->paginate();

        return ZBuilder::make('table')
            ->setPageTitle('网站列表') // 设置页面标题
            ->setTableName('domain') // 设置数据表名
            ->setSearchArea([
                ['text', 'w.domain', '网站', 'like']
            ])
            ->addColumns([ // 批量添加列
                ['wid', 'WID'],
                ['id', 'ID'],
                ['domain', '域名'],
                ['status', '状态', 'status', '', $this->status],
                ['ip_api', 'ip统计接口'],
                ['order_api', '订单统计接口'],
                ['token', 'token'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('delete') // 批量添加顶部按钮
            ->addRightButtons([
                'enable' => ['title' => '启用'],
                'disable' => ['title' => '停止'],
                'edit',
                'delete'
            ]) // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->setColumnWidth([
                '_checkbox' => 10,
                'wid' => 20,
                'id' => 20,
                'domain' => 40,
                'status' => 30,
                'ip_api' => 80,
                'order_api' => 80,
                'token' => 50,
                'right_button' => 35,
            ])
            ->fetch(); // 渲染页面
    }

    public function edit($id = '')
    {
        $find = WebModel::alias('w')
            ->join('domain d', 'w.domain=d.domain', 'left')
            ->field('d.*,w.domain,w.id')
            ->where('w.id', $id)
            ->find();

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!empty($data['type'])){
                if ($find['domain'] != $data['domain']){
                    DomainHistory::create(['domain_id'=>$id,'domain'=>$find['domain'],'add_time'=>date('Y-m-d H:i:s')]);
                    WebModel::where('id',$data['website_id'])->update(['domain'=>$data['domain']]);
                }
                if (DoModel::where('website_id',$data['website_id'])->update($data)) {
                    $this->success('修改成功', url('index'));
                } else {
                    $this->error('修改失败');
                }
            }else{
                if (DoModel::create($data)) {
                    $this->success('新增成功', url('index'));
                } else {
                    $this->error('新增失败');
                }
            }
        }
        return ZBuilder::make('form')
            ->setPageTitle('编辑')
            ->addHidden('website_id',$find['id'])
            ->addHidden('type',$find['website_id']?1:0)
            ->addText('domain', '网站域名', '二级域名【www.baidu.com】，与网站的目录需保持一致',  $find['domain'])
            ->addRadio('status','状态','',['0'=>'停用','1'=>'启用'],'1')
            ->addText('ip_api', 'ip统计接口','',$find['ip_api'])
            ->addText('order_api', '订单统计接口','',$find['order_api'])
            ->addText('token', '接口Token','',$find['token'])
            ->fetch();
    }
}